package org.jeecg.modules.rental.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.modules.rental.entity.RentalScenicSpot;
import org.jeecg.modules.rental.entity.RentalScenicSpotRoute;
import org.jeecg.modules.rental.mapper.RentalScenicSpotMapper;
import org.jeecg.modules.rental.mapper.RentalScenicSpotRouteMapper;
import org.jeecg.modules.rental.service.IRentalScenicSpotRouteService;
import org.jeecg.modules.rental.vo.PolylineVO;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 景点线路规划
 * @Author: jeecg-boot
 * @Date:   2024-07-13
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class RentalScenicSpotRouteServiceImpl extends ServiceImpl<RentalScenicSpotRouteMapper, RentalScenicSpotRoute> implements IRentalScenicSpotRouteService {

    private final CacheManager cacheManager;

    private final RentalScenicSpotMapper rentalScenicSpotMapper;

//    private final IRentalRegionService rentalRegionService;

    @Override
    public Boolean add(RentalScenicSpotRoute rentalScenicSpotRoute) {

        List<String> spotIdList =  rentalScenicSpotRoute.getSpotIdList();

        if(ObjectUtil.isNotEmpty(spotIdList) && spotIdList.size() >= 2){
            rentalScenicSpotRoute.setStartSpotId(Long.parseLong(spotIdList.get(0)) );
            rentalScenicSpotRoute.setEndSpotId(Long.parseLong(spotIdList.get(spotIdList.size() -1)));
        }

        List<RentalScenicSpot> scenicSpotList = rentalScenicSpotMapper.selectBatchIds(rentalScenicSpotRoute.getSpotIdList());
        String spotId = rentalScenicSpotRoute.getSpotIdList().stream().collect(Collectors.joining(","));
        String spotName = scenicSpotList.stream().map(RentalScenicSpot::getName).collect(Collectors.joining(","));
        rentalScenicSpotRoute.setSpotIds(spotId);
        rentalScenicSpotRoute.setSpotNames(spotName);
        return this.save(rentalScenicSpotRoute);
    }

    @Override
    public Boolean edit(RentalScenicSpotRoute rentalScenicSpotRoute) {
        List<RentalScenicSpot> scenicSpotList = rentalScenicSpotMapper.selectBatchIds(rentalScenicSpotRoute.getSpotIdList());
        String spotId = rentalScenicSpotRoute.getSpotIdList().stream().collect(Collectors.joining(","));
        String spotName = scenicSpotList.stream().map(RentalScenicSpot::getName).collect(Collectors.joining(","));
        rentalScenicSpotRoute.setSpotIds(spotId);
        rentalScenicSpotRoute.setSpotNames(spotName);
        return this.updateById(rentalScenicSpotRoute);
    }


    @Override
    public PolylineVO polylineById(String id) {
        PolylineVO scenicSpotRouteVO = new PolylineVO();
        scenicSpotRouteVO.setId(id);
        RentalScenicSpotRoute scenicSpotRoute = baseMapper.selectById(id);
        RentalScenicSpot startSpot = rentalScenicSpotMapper.selectById(scenicSpotRoute.getStartSpotId());

        scenicSpotRouteVO.setFrom(startSpot.getLatitude()+","+startSpot.getLongitude());

        RentalScenicSpot endSpot = rentalScenicSpotMapper.selectById(scenicSpotRoute.getEndSpotId());
        scenicSpotRouteVO.setTo(endSpot.getLatitude()+","+endSpot.getLongitude());

        List<String> spotIds = Arrays.asList(scenicSpotRoute.getSpotIds().split(","));
        List<RentalScenicSpot> scenicSpotList = rentalScenicSpotMapper.selectBatchIds(spotIds);
        String points = scenicSpotList.stream().map(a-> a.getLatitude()+","+a.getLongitude()).collect(Collectors.joining(";"));
        scenicSpotRouteVO.setPoints(points);
        return scenicSpotRouteVO;
    }

    @Override
    public Boolean savePolygon(RentalScenicSpotRoute rentalScenicSpotRoute) {
        RentalScenicSpotRoute entity = baseMapper.selectById(rentalScenicSpotRoute.getId());
        entity.setPolyline(rentalScenicSpotRoute.getPolyline());
        return this.updateById(entity);
    }

    @Override
    public List<RentalScenicSpotRoute> queryByLocation(String latitude, String longitude) {
        //通过经纬度获取最近的区域
//        RegionCacheVO rentalRegion = rentalRegionService.selectOneByLocation(latitude, longitude, OrgThreadHolder.get());
        List<RentalScenicSpotRoute> scenicSpotRouteList = baseMapper.selectList(
                new QueryWrapper<RentalScenicSpotRoute>());
        if(ObjectUtil.isNotEmpty(scenicSpotRouteList)){
            List<Long> startSpotIds = scenicSpotRouteList.stream().map(RentalScenicSpotRoute::getStartSpotId).collect(Collectors.toList());
            List<Long> endSpotIds = scenicSpotRouteList.stream().map(RentalScenicSpotRoute::getEndSpotId).collect(Collectors.toList());
            startSpotIds.addAll(endSpotIds);
            List<RentalScenicSpot> rentalScenicSpotList = rentalScenicSpotMapper.selectBatchIds(startSpotIds);
            scenicSpotRouteList.stream().forEach(item->{
                RentalScenicSpot startScenicSpot = rentalScenicSpotList.stream().filter(a-> a.getId().equals(item.getStartSpotId())).findFirst().orElse(null);
                if(ObjectUtil.isNotEmpty(startScenicSpot)){
                    item.setStartSpotName(startScenicSpot.getName());
                }
                RentalScenicSpot endScenicSpot = rentalScenicSpotList.stream().filter(a-> a.getId().equals(item.getEndSpotId())).findFirst().orElse(null);
                if(ObjectUtil.isNotEmpty(endScenicSpot)){
                    item.setEndSpotName(endScenicSpot.getName());
                }
            });
        }
        return scenicSpotRouteList;
    }

    @Override
    public IPage<RentalScenicSpotRoute> selectJoinPage(Page page, RentalScenicSpotRoute rentalScenicSpotRoute) {
        IPage<RentalScenicSpotRoute> pageList = baseMapper.selectJoinPage(page, rentalScenicSpotRoute);
        if(ObjectUtil.isNotEmpty(pageList.getRecords())){
            List<Long> endSpotIdList = pageList.getRecords().stream().map(RentalScenicSpotRoute::getEndSpotId).collect(Collectors.toList());
            List<Long> startSpotIdList = pageList.getRecords().stream().map(RentalScenicSpotRoute::getStartSpotId).collect(Collectors.toList());
            endSpotIdList.addAll(startSpotIdList);
            List<RentalScenicSpot> scenicSpotList = rentalScenicSpotMapper.selectBatchIds(endSpotIdList);
            Cache deptCache = cacheManager.getCache(CacheConstant.SYS_DEPARTS_CACHE);
            pageList.getRecords().stream().forEach(item->{
                if(ObjectUtil.isNotEmpty(deptCache)){
                    //TODO 优化缓存
//                    SysDepartVO sysDepartVO = deptCache.get(item.getSysOrgCode(), SysDepartVO.class);
//                    if(ObjectUtil.isNotEmpty(sysDepartVO)){
//                        item.setSysOrgName(sysDepartVO.getDepartName());
//                    }
                }

                RentalScenicSpot startScenicSpot = scenicSpotList.stream()
                        .filter(a-> a.getId().equals(item.getStartSpotId())).findFirst().orElse(null);
                if(ObjectUtil.isNotEmpty(startScenicSpot)){
                    item.setStartSpotName(startScenicSpot.getName());
                }

                RentalScenicSpot endScenicSpot = scenicSpotList.stream()
                        .filter(a-> a.getId().equals(item.getEndSpotId())).findFirst().orElse(null);
                if(ObjectUtil.isNotEmpty(endScenicSpot)){
                    item.setEndSpotName(endScenicSpot.getName());
                }

                if(StrUtil.isNotBlank(item.getSpotIds())){
                    item.setSpotIdList(Arrays.asList(item.getSpotIds().split(",")));
                }
            });
        }
        return pageList;
    }


}
